package com.jzcn.admin.controller;

import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.log.LoginLogDTO;
import com.jzcn.common.enums.ActionEnum;
import com.jzcn.common.response.ResponseCode;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.IpUtil;
import com.jzcn.domain.SysLoginLog;
import com.jzcn.domain.SysUser;
import com.jzcn.service.SysLoginLogService;
import com.jzcn.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@Api(value = "public", tags = "用户退出")
@RequestMapping("/api/public")
public class LoginController extends BaseController {

    @Resource
    SysLoginLogService sysLoginLogService;

    @Resource
    SysUserService sysUserService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseResult login(@RequestParam("username") @ApiParam("登录名") String username,
                                @RequestParam("password") @ApiParam("密码") String password, HttpServletRequest request) {
        SysUser sysUser = sysUserService.login(username,password);
        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUserName(), password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        SysUser users = (SysUser) subject.getPrincipal();
        LoginLogDTO loginLog = new LoginLogDTO();
        loginLog.setUserId(users.getId());
        loginLog.setCreateTime(new Date());
        loginLog.setMsg("登录成功");
        loginLog.setActionType(ActionEnum.ACTION_LOGIN.getValue());
        loginLog.setActionName(ActionEnum.ACTION_LOGIN.getContent());
        loginLog.setIp(IpUtil.getIp(request));
        sysLoginLogService.saveLoginLog(loginLog);
        return ResponseResult.success(users);

    }

    @GetMapping("/unLogin")
    public ResponseResult unLogin() {
        return ResponseResult.failure(ResponseResult.NO_LOGIN,"登录超时，请重新登录");
    }

    @GetMapping("/noPermissions")
    public ResponseResult unPerm() {
        return ResponseResult.failure(ResponseResult.UNAUTHORIZED,"您没有权限访问!");
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public ResponseResult loginOut(HttpServletRequest request) {
        LoginLogDTO loginLog = new LoginLogDTO();
        loginLog.setUserId(this.getUserId());
        loginLog.setCreateTime(new Date());
        loginLog.setMsg("退出成功");
        loginLog.setActionType(ActionEnum.ACTION_UNLOGIN.getValue());
        loginLog.setActionName(ActionEnum.ACTION_UNLOGIN.getContent());
        loginLog.setIp(IpUtil.getIp(request));
        sysLoginLogService.saveLoginLog(loginLog);
        this.clearCachedAuthorizationInfo();
        this.logout();
        return ResponseResult.success();
    }
}
